package cn.lichenes.service.impl;

import cn.lichenes.entity.User;
import cn.lichenes.entity.UserStatus;
import cn.lichenes.repository.IndentRepository;
import cn.lichenes.repository.PhoneRepository;
import cn.lichenes.repository.UserRepository;
import cn.lichenes.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private PhoneRepository phoneRepository;
    @Autowired
    private IndentRepository indentRepository;

    public Page<User> findAllUser(String username, int page) {
        PageRequest pageRequest = PageRequest.of(page-1, 10, Sort.by(Sort.Direction.DESC,"uTime"));
        Specification<User> specification = new Specification<User>() {
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.lt(root.get("userStatus").get("id").as(Integer.class),2);
                if (username != null){
                    Predicate predicate1 = criteriaBuilder.and(predicate,criteriaBuilder.like(root.get("uName").as(String.class),"%"+username+"%"));
                    return predicate1;
                }
                return predicate;
            }
        };
        return userRepository.findAll(specification,pageRequest);
    }

    @Transactional
    public boolean banUser(Integer id, Integer status) {
        UserStatus userStatus = new UserStatus();
        User user = userRepository.findById(id).get();
        if (status == 1){
            userStatus.setId(0);
            userStatus.setName("封禁");
            user.setUserStatus(userStatus);
            userRepository.save(user);
        }else {
            userStatus.setId(1);
            userStatus.setName("用户");
            user.setUserStatus(userStatus);
            userRepository.save(user);
        }
        return true;
    }

    public boolean addUser(User user) {
        User user1 = userRepository.save(user);
        if (user1 != null){
            return true;
        }
        return false;
    }

    public boolean updateUser(Integer id, String name, String phone, String email) {
        User user = userRepository.findById(id).get();
        user.setuName(name);
        user.setuPhone(phone);
        user.setuEmail(email);
        User save = userRepository.save(user);
        if (save != null){
            return true;
        }
        return false;
    }

    public Map<String, Object> getIndex() {
        long user = userRepository.count();
        long phone = phoneRepository.count();
        long indent = indentRepository.count();
        Map<String,Object> map = new HashMap<>();
        map.put("user",user);
        map.put("phone",phone);
        map.put("indent",indent);
        return map;
    }

    public Boolean judgeEmail(String email) {
        User user = userRepository.findByuEmail(email);
        if (user != null){
            return false;
        }
        return true;
    }

    public Boolean judgeAccount(String name) {
        User user = userRepository.findByuAccount(name);
        if (user != null){
            return false;
        }
        return true;
    }

    public Boolean judgePhone(String phone) {
        User user = userRepository.findByuPhone(phone);
        if (user != null){
            return false;
        }
        return true;
    }
}
